<template>
  <div class="home">
    <!-- 搜索区域 -->
    <div class="search-section">
      <div class="search-box">
        <el-input
          v-model="searchKeyword"
          placeholder="搜索餐厅名称"
          class="search-input"
          clearable
        >
          <template #prefix>
            <el-icon><Search /></el-icon>
          </template>
        </el-input>
        <el-button type="primary" @click="handleSearch">搜索</el-button>
      </div>
    </div>

    <!-- 餐厅列表 -->
    <div class="section">
      <div class="section-header">
        <h2>推荐餐厅</h2>
        <el-button
          type="primary"
          link
          @click="router.push('/restaurant/list')"
        >
          查看更多 <el-icon><ArrowRight /></el-icon>
        </el-button>
      </div>
      <div class="restaurant-list">
        <el-card
          v-for="restaurant in restaurants"
          :key="restaurant.id"
          class="restaurant-item"
          @click="handleRestaurantClick(restaurant)"
        >
          <el-image :src="restaurant.image" fit="cover" class="restaurant-img" />
          <div class="restaurant-info">
            <h3>{{ restaurant.name }}</h3>
            <p class="address">
              <el-icon><Location /></el-icon>
              {{ restaurant.address }}
            </p>
            <div class="meta">
              <el-tag
                :type="restaurant.status === 1 ? 'success' : 'danger'"
                size="small"
              >
                {{ restaurant.status === 1 ? '营业中' : '休息中' }}
              </el-tag>
              <span class="business-hours">{{ restaurant.businessHours }}</span>
            </div>
          </div>
        </el-card>
      </div>
    </div>

    <!-- 热门菜品 -->
    <div class="section">
      <div class="section-header">
        <h2>热门菜品</h2>
      </div>
      <div class="dish-list">
        <el-card
            v-for="dish in hotDishes"
            :key="dish.id"
            class="dish-item"
            :body-style="{ padding: '0px' }"
        >
          <el-image :src="dish.image" fit="cover" class="dish-img" />
          <div class="dish-info">
            <h3>{{ dish.name }}</h3>
            <p class="restaurant-name">{{ dish.restaurantName }}</p>
            <p class="price">¥{{ dish.price }}</p>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Search, Location, ArrowRight } from '@element-plus/icons-vue'
import { getRestaurantList } from '@/api/restaurant'
import { getHotDishes } from '@/api/dish'
import { ElMessage } from 'element-plus'

const router = useRouter()
const restaurants = ref([])
const hotDishes = ref([])
const searchKeyword = ref('')
const loading = ref(false)

// 获取餐厅列表
const fetchRestaurants = async () => {
  try {
    loading.value = true
    const res = await getRestaurantList({
      page: 1,
      pageSize: 8,
      name: searchKeyword.value
    })
    restaurants.value = res.data.records
  } catch (error) {
    console.error('获取餐厅列表失败:', error)
    ElMessage.error('获取餐厅列表失败')
  } finally {
    loading.value = false
  }
}

// 获取热门菜品
const fetchHotDishes = async () => {
  try {
    const res = await getHotDishes({ limit: 8 })
    hotDishes.value = res.data
  } catch (error) {
    console.error('获取热门菜品失败:', error)
  }
}

// 处理搜索
const handleSearch = () => {
  fetchRestaurants()
}

// 点击餐厅卡片
const handleRestaurantClick = (restaurant) => {
  router.push(`/restaurant/${restaurant.id}`)
}

onMounted(() => {
  fetchRestaurants()
  fetchHotDishes()
})
</script>

<style scoped lang="scss">
.home {
  padding: 20px;
}

.search-section {
  margin-bottom: 40px;
  display: flex;
  justify-content: center;

  .search-box {
    width: 600px;
    display: flex;
    gap: 15px;

    .search-input {
      flex: 1;
    }
  }
}

.section {
  margin-bottom: 40px;

  .section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;

    h2 {
      margin: 0;
      font-size: 24px;
      font-weight: bold;
    }
  }
}

.restaurant-list {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 25px;
}

.restaurant-item {
  cursor: pointer;
  transition: all 0.3s;
  border: none;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 20px rgba(0,0,0,0.15);
  }

  .restaurant-img {
    width: 100%;
    height: 200px;
    object-fit: cover;
  }

  .restaurant-info {
    padding: 15px;

    h3 {
      margin: 0 0 10px;
      font-size: 18px;
      font-weight: bold;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .address {
      display: flex;
      align-items: center;
      gap: 5px;
      margin: 0 0 10px;
      color: #666;
      font-size: 14px;
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
    }

    .meta {
      display: flex;
      align-items: center;
      justify-content: space-between;

      .business-hours {
        color: #666;
        font-size: 14px;
      }
    }
  }
}

.dish-list {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 25px;
}

.dish-item {
  cursor: pointer;
  transition: all 0.3s;
  border: none;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);

  &:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 20px rgba(0,0,0,0.15);
  }

  .dish-img {
    width: 100%;
    height: 200px;
    object-fit: cover;
  }

  .dish-info {
    padding: 15px;

    h3 {
      margin: 0 0 8px;
      font-size: 18px;
      font-weight: bold;
    }

    .restaurant-name {
      color: #666;
      font-size: 14px;
      margin: 0 0 8px;
    }

    .price {
      color: #f56c6c;
      margin: 0;
      font-size: 18px;
      font-weight: bold;
    }
  }
}
</style>
